<template>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="1200px">
        <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="100px">
            <el-form-item label="商品SKU" prop="sku">
                    <el-input v-model.trim="queryParams.sku" placeholder="请输入商品SKU" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                </el-form-item>
            <el-form-item label="商品SPU" prop="spu">
                <el-input v-model.trim="queryParams.spu" placeholder="请输入商品SPU" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="仓库" prop="warehouse_id">
                <el-select v-model="queryParams.warehouse_id" placeholder="请选择仓库" clearable style="width: 240px">
                    <el-option v-for="(dict, index) in dict.warehouseList" :key="index" :label="dict.warehouse_name" :value="dict.warehouse_id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label=" ">
                <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-table v-loading="loading" :data="tableData" border script>
            <el-table-column label="ID" prop="id" min-width="60"></el-table-column>
            <el-table-column label="仓库" prop="warehouse_id" min-width="160">
                <template slot-scope="scope">
                    {{ scope.row.warehouse_id | formatLabelByValueFuzzy(dict.warehouseList, { label: 'warehouse_name', value: 'warehouse_id' }) }}
                </template>
            </el-table-column>
            <el-table-column label="商品SKU" prop="sku" min-width="160"></el-table-column>
            <el-table-column label="商品SPU" prop="spu" min-width="160"></el-table-column>
            <el-table-column label="产品图片" min-width="120">
                <template slot-scope="scope">
                    <ImageUploadImg v-model="scope.row.product_images" :readOnly="true"></ImageUploadImg>
                </template>
            </el-table-column>

            <el-table-column label="库存数量" prop="new_quantity" min-width="120"></el-table-column>
            <el-table-column label="创建时间" prop="created_time" min-width="160"></el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList" />
    </el-dialog>
</template>

<script>
import { queryProductOutSpuInfo, getWarehouse } from '@/api/product'
export default {
    name: 'ProductOutSpuListDialog',
    components: {},
    props: {
        value: {
            type: Boolean,
            default: false,
            required: true,
        },
        params: {
            type: Object,
            default: () => {},
        },
    },
    data() {
        return {
            loading: false,
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],
            queryParams: {
                spu: undefined,
                warehouse_id: undefined,
            },
            pageInfo: {
                page: 1,
                limit: 10,
            },
            dict: {
                warehouseList: [],
            },
            total_new_quantity: 0,
        }
    },
    computed: {
        title() {
            return `SPU详情`
        },
        dialogVisible: {
            get() {
                return this.value
            },
            set(val) {
                this.$emit('input', val)
            },
        },
    },
    async mounted() {
        this.loading = true
        try {
            const warehouseData = await getWarehouse()
            this.dict.warehouseList = warehouseData?.data ?? []
        } catch (error) {
            this.loading = false
        }
        this.queryParams.spu = this.params.spu
        this.queryParams.warehouse_id = this.params.warehouse_id
        this.getList()
    },
    methods: {
        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
        showSkuEditDialog(row) {
            this.skuEditDialog.visible = true
            this.skuEditDialog.data = row
        },
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            queryProductOutSpuInfo(qyparams)
                .then(res => {
                    this.tableData = res.data?.list ?? []
                    this.total = res.data?.total ?? 0
                })
                .finally(() => {
                    this.loading = false
                })
        },
    },
}
</script>

<style></style>
